package com.zzt.video.controller;

import com.zzt.video.model.dto.*;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/data")
public class DataController {

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/home_data")
    public ResponseEntity<HomeDataDto> getHomeData() {
        HomeDataDto dataDto = new HomeDataDto();
        RBucket<List<AdvertiseShowDto>> advertiseBucket = redissonClient.getBucket("advertise:list");
        List<AdvertiseShowDto> advertiseDtos = advertiseBucket.get();
        dataDto.setAdvertiseDtos(advertiseDtos);
        List<CategoryDto> categoryDtos = new ArrayList<>();
        String[] titles = {"日本视频","国产视频","自慰自拍","制服和cos","欧美"};
        int typeList[] = {2,1,3,4,5};
        for(int i : typeList) {
            CategoryDto categoryDto = new CategoryDto();
            categoryDto.setCategoryTitle(titles[i-1]);
            RBucket<List<SeriesDto>> seriesBucket = redissonClient.getBucket("series:latest:" + i);
            List<SeriesDto> series = seriesBucket.get();
            RBucket<List<RankListDto>> rankListBucket = redissonClient.getBucket("series:rank:weekly:" + i);
            List<RankListDto> rankListDtos = rankListBucket.get();
            categoryDto.setSeriesDtos(series);
            categoryDto.setRankListDtos(rankListDtos);
            categoryDtos.add(categoryDto);
        }
        dataDto.setCategoryDtos(categoryDtos);
        return ResponseEntity.ok(dataDto);
    }
}
